package xin.vote.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import xin.vote.model.Votes;
import xin.vote.service.IVoteService;

@Controller
@RequestMapping("/uservote")
public class VoteController {
	@Resource
	private IVoteService voteService;
	
	@RequestMapping("/addvote")
	@ResponseBody
	public String updateSale(@RequestParam(value = "matchid") Integer matchid,
			@RequestParam(value = "battleid") String battleid,
			@RequestParam(value = "playerid") Integer playerid,
			@RequestParam(value = "userid") Integer userid) {
		Votes v = new Votes();
		v.setMatchid(matchid);
		v.setBattleid(battleid);
		v.setPlayerid(playerid);
		v.setUserid(userid);
		Votes vote = voteService.findRepeat(v);//查重，限制重复投
		String msg = null;
		if(vote==null) {
			int ret = voteService.addvote(v);
			if (ret > 0) {
				msg="1";//投票成功
//				findAllPlayerAndVote();
			} else {
				msg="0";//投票失败
			}
		}else {
			msg="2";//已投过本轮票
		}
		return msg;
	}
	
	//result 
//	@RequestMapping("/result")
//	public ModelAndView findAllPlayerAndVote() {// defaultValue="0",这里是null
//		List<Votes> playeAndVotes = voteService.findAllPlayerAndVote();
//		ModelAndView mav = new ModelAndView("users/list");
//		
//		mav.addObject("playervote",JSON.toJSONString(playeAndVotes));
//		return mav;
//	}
	
	@RequestMapping("/result")
	@ResponseBody
	public List<Votes> findAllPlayerAndVote() {// defaultValue="0",这里是null
		List<Votes> playervote = voteService.findAllPlayerAndVote();
		
		return playervote;
	}
	
}
